
<!DOCTYPE HTML>
<html lang="zh-hans" >
    <head>
        <meta charset="UTF-8">
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <title>基础功能 · Cup Documentation</title>
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="description" content="">
        <meta name="generator" content="GitBook 3.2.3">
        <meta name="author" content="Guannan Ma">
        
        
    
    <link rel="stylesheet" href="../../gitbook/style.css">

    
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-splitter/splitter.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-anchors/plugin.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-insert-logo/plugin.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-highlight/website.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-search/search.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-fontsettings/website.css">
                
            
        

    

    
        
    
        
    
        
    
        
    
        
    
        
    

        
    
    
    
    
    <meta name="HandheldFriendly" content="true"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../../gitbook/images/apple-touch-icon-precomposed-152.png">
    <link rel="shortcut icon" href="../../gitbook/images/favicon.ico" type="image/x-icon">

    
    <link rel="next" href="CommonService.html" />
    
    

    <style>
    @media only screen and (max-width: 640px) {
        .book-header .hidden-mobile {
            display: none;
        }
    }
    </style>
    <script>
        window["gitbook-plugin-github-buttons"] = {"repo":"baidu/CUP","types":["star","watch","fork"],"size":"large"};
    </script>

    
        <link rel="shortcut icon" href='../images/cup.logo.jpg' type="image/x-icon">
    
    
        <link rel="bookmark" href='../images/cup.logo.jpg' type="image/x-icon">
    
    
        <link rel="apple-touch-icon" href='../images/cup.logo.jpg'>
    
    

    </head>
    <body>
        
<div class="book">
    <div class="book-summary">
        
            
<div id="book-search-input" role="search">
    <input type="text" placeholder="输入并搜索" />
</div>

            
                <nav role="navigation">
                


<ul class="summary">
    
    
    
        
        <li>
            <a href="https://github.com/baidu/CUP" target="_blank" class="custom-link">Github Website</a>
        </li>
    
    

    
    <li class="divider"></li>
    

    
        
        
    
        <li class="chapter " data-level="1.1" data-path="../">
            
                <a href="../">
            
                    
                    CUP概述
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2" >
            
                <span>
            
                    
                    用户案例
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter active" data-level="1.2.1" data-path="CommonUserSenario.html">
            
                <a href="CommonUserSenario.html">
            
                    
                    基础功能
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.2" data-path="CommonService.html">
            
                <a href="CommonService.html">
            
                    
                    通用服务
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.3" data-path="GeneralService.html">
            
                <a href="GeneralService.html">
            
                    
                    快速构建PythonService
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.4" data-path="AsyncNet.html">
            
                <a href="AsyncNet.html">
            
                    
                    异步网络通讯协议
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.3" >
            
                <a target="_blank" href="http://cupdoc.iobusy.com">
            
                    
                    官方文档
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4" data-path="../CommitCode.html">
            
                <a href="../CommitCode.html">
            
                    
                    参与社区
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5" data-path="../DevMembers.html">
            
                <a href="../DevMembers.html">
            
                    
                    开发人员
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6" >
            
                <span>
            
                    
                    开发计划
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.6.1" data-path="../RoadMap.html">
            
                <a href="../RoadMap.html">
            
                    
                    RoadMap
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.2" data-path="../ChangeLog.html">
            
                <a href="../ChangeLog.html">
            
                    
                    ReleaseNote
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    

    

    <li class="divider"></li>

    <li>
        <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
            本书使用 GitBook 发布
        </a>
    </li>
</ul>


                </nav>
            
        
    </div>

    <div class="book-body">
        
            <div class="body-inner">
                
                    

<div class="book-header" role="navigation">
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href=".." >基础功能</a>
    </h1>
</div>




                    <div class="page-wrapper" tabindex="-1" role="main">
                        <div class="page-inner">
                            
<div id="book-search-results">
    <div class="search-noresults">
    
                                <section class="normal markdown-section">
                                
                                <!-- MDTOC maxdepth:6 firsth1:1 numbering:0 flatten:0 bullets:1 updateOnSave:1 -->
<ul>
<li><a href="#1-&#x5E38;&#x7528;&#x64CD;&#x4F5C;">1. &#x5E38;&#x7528;&#x64CD;&#x4F5C;</a><ul>
<li><a href="#11-cupmail-&#x53D1;&#x9001;&#x90AE;&#x4EF6;&#x7C7B;">1.1 cup.mail &#x53D1;&#x9001;&#x90AE;&#x4EF6;&#x7C7B;</a></li>
<li><a href="#12-cuplog-&#x7B80;&#x5355;&#x597D;&#x7528;&#x7684;cup-log&#x51FD;&#x6570;">1.2 cup.log &#x7B80;&#x5355;&#x597D;&#x7528;&#x7684;cup log&#x51FD;&#x6570;</a></li>
<li><a href="#13-cupres-&#x83B7;&#x53D6;linux&#x673A;&#x5668;&#x8D44;&#x6E90;&#x7C7B;&#x76F8;&#x5173;&#x4FE1;&#x606F;">1.3 cup.res &#x83B7;&#x53D6;linux&#x673A;&#x5668;&#x8D44;&#x6E90;&#x7C7B;&#x76F8;&#x5173;&#x4FE1;&#x606F;</a></li>
<li><a href="#14-cupshell-&#x64CD;&#x4F5C;shell&#x547D;&#x4EE4;&#x76F8;&#x5173;&#x7684;package">1.4 cup.shell &#x64CD;&#x4F5C;shell&#x547D;&#x4EE4;&#x76F8;&#x5173;&#x7684;package.</a></li>
<li><a href="#15-cuputil-util&#x7C7B;package-Rich-ConfigurationContext">1.5 cup.util util&#x7C7B;package - Rich Configuration/Context</a></li>
<li><a href="#16-cupnet-&#x7F51;&#x7EDC;&#x64CD;&#x4F5C;&#x76F8;&#x5173;&#x7684;package">1.6 cup.net &#x7F51;&#x7EDC;&#x64CD;&#x4F5C;&#x76F8;&#x5173;&#x7684;package.</a><ul>
<li><a href="#161-&#x7F51;&#x5361;&#x4FE1;&#x606F;&#x83B7;&#x53D6;&#x8DEF;&#x7531;&#x4FE1;&#x606F;&#x83B7;&#x53D6;socket&#x8BBE;&#x7F6E;">1.6.1 &#x7F51;&#x5361;&#x4FE1;&#x606F;&#x83B7;&#x53D6;&#x3001;&#x8DEF;&#x7531;&#x4FE1;&#x606F;&#x83B7;&#x53D6;&#x3001;socket&#x8BBE;&#x7F6E;</a></li>
<li><a href="#162-&#x5F02;&#x6B65;&#x7F51;&#x7EDC;&#x6D88;&#x606F;&#x901A;&#x4FE1;&#x5E93;">1.6.2 &#x5F02;&#x6B65;&#x7F51;&#x7EDC;&#x6D88;&#x606F;&#x901A;&#x4FE1;&#x5E93;</a></li>
</ul>
</li>
<li><a href="#17-cupdecorators-&#x8FDB;&#x884C;&#x51FD;&#x6570;&#x7C7B;&#x4FEE;&#x9970;&#x7684;module">1.7 cup.decorators &#x8FDB;&#x884C;&#x51FD;&#x6570;&#x3001;&#x7C7B;&#x4FEE;&#x9970;&#x7684;module</a></li>
<li><a href="#18-cupjenkinslib-&#x4E0E;&#x6301;&#x7EED;&#x96C6;&#x6210;&#x5E73;&#x53F0;&#x8FDB;&#x884C;API&#x64CD;&#x4F5C;">1.8 cup.jenkinslib &#x4E0E;&#x6301;&#x7EED;&#x96C6;&#x6210;&#x5E73;&#x53F0;&#x8FDB;&#x884C;API&#x64CD;&#x4F5C;</a></li>
<li><a href="#19-cupexfile">1.9 cup.exfile</a></li>
<li><a href="#110-cupthirdp-&#x7B2C;&#x4E09;&#x65B9;&#x7684;&#x5E93;&#x4F9D;&#x8D56;&#x652F;&#x6301;">1.10 cup.thirdp &#x7B2C;&#x4E09;&#x65B9;&#x7684;&#x5E93;&#x4F9D;&#x8D56;&#x652F;&#x6301;</a></li>
</ul>
</li>
</ul>
<!-- /MDTOC -->
<h1 id="1-&#x5E38;&#x7528;&#x64CD;&#x4F5C;"><a name="1-&#x5E38;&#x7528;&#x64CD;&#x4F5C;" class="plugin-anchor" href="#1-&#x5E38;&#x7528;&#x64CD;&#x4F5C;"><i class="fa fa-link" aria-hidden="true"></i></a>1. &#x5E38;&#x7528;&#x64CD;&#x4F5C;</h1>
<p>&#x76EE;&#x524D;&#x57FA;&#x7840;&#x5E93;&#x5305;&#x542B;&#x5982;&#x4E0B;&#x5927;&#x7684;Package[&#x6BCF;&#x7C7B;&#x5305;&#x542B;&#x82E5;&#x5E72;&#x5C0F;&#x7684;package&#x53CA;module].</p>
<p><strong><em>&#x6392;&#x5E8F;&#x6309;&#x7167;&#x5E38;&#x7528;-&gt;&#x8F83;&#x5E38;&#x7528;-&gt;&#x4E13;&#x7528;</em></strong></p>
<h2 id="11-cupmail-&#x53D1;&#x9001;&#x90AE;&#x4EF6;&#x7C7B;"><a name="11-cupmail-&#x53D1;&#x9001;&#x90AE;&#x4EF6;&#x7C7B;" class="plugin-anchor" href="#11-cupmail-&#x53D1;&#x9001;&#x90AE;&#x4EF6;&#x7C7B;"><i class="fa fa-link" aria-hidden="true"></i></a>1.1 cup.mail &#x53D1;&#x9001;&#x90AE;&#x4EF6;&#x7C7B;</h2>
<p>&#x5177;&#x4F53;&#x6587;&#x6863;&#x4ECB;&#x7ECD;</p>
<blockquote>
<p><a href="http://cupdoc.iobusy.com/cup/#module-cup.mail" target="_blank">http://cupdoc.iobusy.com/cup/#module-cup.mail</a></p>
</blockquote>
<p>&#x4EE3;&#x7801;&#x793A;&#x4F8B;&#xFF1A;</p>
<pre><code class="lang-python"><span class="hljs-keyword">from</span> cup <span class="hljs-keyword">import</span> mail
mailer = mail.SmtpMailer(
    <span class="hljs-string">&apos;xxx@xxx.com&apos;</span>,   <span class="hljs-comment"># sender</span>
    <span class="hljs-string">&apos;xxxx.smtp.xxx.com&apos;</span>,  <span class="hljs-comment"># smtp server</span>
    is_html=<span class="hljs-keyword">True</span>   <span class="hljs-comment"># use html or not for email contents</span>
)
mailer.sendmail(
    [
        <span class="hljs-string">&apos;abc@baidu.com&apos;</span>,
        <span class="hljs-string">&apos;xxx@baidu.com&apos;</span>,
        <span class="hljs-string">&apos;yyy@baidu.com&apos;</span>
    ],
    <span class="hljs-string">&apos;test_img&apos;</span>,
    (
        <span class="hljs-string">&apos;testset &lt;img src=&quot;cid:screenshot.png&quot;&gt;&lt;/img&gt;&apos;</span>
    ),
    <span class="hljs-comment"># attachment</span>
    [
        <span class="hljs-string">&apos;/home/work/screenshot.png&apos;</span>,
        <span class="hljs-string">&apos;../abc.zip&apos;</span>
    ]
)
</code></pre>
<h2 id="12-cuplog-&#x7B80;&#x5355;&#x597D;&#x7528;&#x7684;cup-log&#x51FD;&#x6570;"><a name="12-cuplog-&#x7B80;&#x5355;&#x597D;&#x7528;&#x7684;cup-log&#x51FD;&#x6570;" class="plugin-anchor" href="#12-cuplog-&#x7B80;&#x5355;&#x597D;&#x7528;&#x7684;cup-log&#x51FD;&#x6570;"><i class="fa fa-link" aria-hidden="true"></i></a>1.2 cup.log &#x7B80;&#x5355;&#x597D;&#x7528;&#x7684;cup log&#x51FD;&#x6570;</h2>
<blockquote>
<p>&#x53BB;&#x9664;&#x590D;&#x6742;log&#x8BBE;&#x7F6E;&#xFF0C;&#x5373;&#x5F00;&#x5373;&#x7528;</p>
</blockquote>
<p> &#x5177;&#x4F53;&#x6587;&#x6863;&#x4ECB;&#x7ECD;</p>
<blockquote>
<p><a href="http://cupdoc.iobusy.com/cup/#module-cup.log" target="_blank">http://cupdoc.iobusy.com/cup/#module-cup.log</a></p>
</blockquote>
<p> &#x4EE3;&#x7801;&#x793A;&#x4F8B;&#xFF1A;</p>
<pre><code class="lang-python"><span class="hljs-keyword">import</span> logging
<span class="hljs-keyword">from</span> cup <span class="hljs-keyword">import</span> log
log.init_comlog(
    <span class="hljs-string">&apos;test&apos;</span>,
    log.DEBUG,
    <span class="hljs-string">&apos;/home/work/test/test.log&apos;</span>,
    log.ROTATION,
    <span class="hljs-number">1024</span>,
    <span class="hljs-keyword">False</span>
)
log.info(<span class="hljs-string">&apos;test xxx&apos;</span>)
log.error(<span class="hljs-string">&apos;test critical&apos;</span>)

<span class="hljs-comment"># &#x652F;&#x6301;&#x91CD;&#x65B0;&#x8BBE;&#x7F6E;&#x65E5;&#x5FD7;level&#x3001;&#x65E5;&#x5FD7;&#x6587;&#x4EF6;&#x4F4D;&#x7F6E;&#x3001;&#x53C2;&#x6570;&#x7B49;&#xFF0C;&#x5177;&#x4F53;&#x53C2;&#x6570;&#x8BBE;&#x7F6E;&#x89C1;&#x6587;&#x6863;</span>
</code></pre>
<pre><code class="lang-python">
<span class="hljs-comment"># Level: Datetime * [pid:threadid] [source.code:lineno] content</span>
INFO:    <span class="hljs-number">2017</span><span class="hljs-number">-02</span><span class="hljs-number">-06</span> <span class="hljs-number">19</span>:<span class="hljs-number">35</span>:<span class="hljs-number">54</span>,<span class="hljs-number">242</span> * [<span class="hljs-number">16561</span>:<span class="hljs-number">140543940048640</span>] [log.py:<span class="hljs-number">278</span>] --------------------Log Initialized Successfully--------------------
INFO:    <span class="hljs-number">2017</span><span class="hljs-number">-02</span><span class="hljs-number">-06</span> <span class="hljs-number">19</span>:<span class="hljs-number">35</span>:<span class="hljs-number">54</span>,<span class="hljs-number">242</span> * [<span class="hljs-number">16561</span>:<span class="hljs-number">140543940048640</span>] [cup_log_test.py:<span class="hljs-number">36</span>] info
DEBUG:   <span class="hljs-number">2017</span><span class="hljs-number">-02</span><span class="hljs-number">-06</span> <span class="hljs-number">19</span>:<span class="hljs-number">35</span>:<span class="hljs-number">54</span>,<span class="hljs-number">243</span> * [<span class="hljs-number">16561</span>:<span class="hljs-number">140543940048640</span>] [cup_log_test.py:<span class="hljs-number">40</span>] debug
</code></pre>
<h2 id="13-cupres-&#x83B7;&#x53D6;linux&#x673A;&#x5668;&#x8D44;&#x6E90;&#x7C7B;&#x76F8;&#x5173;&#x4FE1;&#x606F;"><a name="13-cupres-&#x83B7;&#x53D6;linux&#x673A;&#x5668;&#x8D44;&#x6E90;&#x7C7B;&#x76F8;&#x5173;&#x4FE1;&#x606F;" class="plugin-anchor" href="#13-cupres-&#x83B7;&#x53D6;linux&#x673A;&#x5668;&#x8D44;&#x6E90;&#x7C7B;&#x76F8;&#x5173;&#x4FE1;&#x606F;"><i class="fa fa-link" aria-hidden="true"></i></a>1.3 cup.res &#x83B7;&#x53D6;linux&#x673A;&#x5668;&#x8D44;&#x6E90;&#x7C7B;&#x76F8;&#x5173;&#x4FE1;&#x606F;</h2>
<p>&#x4E3E;&#x4F8B;&#xFF0C; &#x83B7;&#x5F97;&#x5F53;&#x524D;&#x673A;&#x5668;cpu/mem/net&#x7EDF;&#x8BA1;&#x4FE1;&#x606F;&#x3002; &#x8FDB;&#x7A0B;&#x8D44;&#x6E90;&#x4FE1;&#x606F;&#xFF08;&#x6240;&#x6709;/proc/pid&#x80FD;&#x62FF;&#x5230;&#x7684;&#x8D44;&#x6E90;&#x4FE1;&#x606F;)
 &#x5177;&#x4F53;&#x6587;&#x6863;&#x4ECB;&#x7ECD;</p>
<blockquote>
<p><a href="http://cupdoc.iobusy.com/cup.res/" target="_blank">http://cupdoc.iobusy.com/cup.res/</a></p>
</blockquote>
<p> &#x4EE3;&#x7801;&#x793A;&#x4F8B;&#xFF1A;</p>
<pre><code class="lang-python"><span class="hljs-comment"># 1. &#x83B7;&#x53D6;&#x7CFB;&#x7EDF;&#x4FE1;&#x606F;&#xFF0C; &#x53EF;&#x4EE5;&#x83B7;&#x53D6;cpu mem network_speed&#x7B49;&#x7B49;</span>
<span class="hljs-keyword">import</span> cup
<span class="hljs-comment"># &#x8BA1;&#x7B97;60&#x5185;cpu&#x7684;&#x4F7F;&#x7528;&#x60C5;&#x51B5;&#x3002;</span>
<span class="hljs-keyword">from</span> cup.res <span class="hljs-keyword">import</span> linux
cpuinfo = linux.get_cpu_usage(intvl_in_sec=<span class="hljs-number">60</span>)
<span class="hljs-keyword">print</span> cpuinfo.usr

<span class="hljs-comment"># get_meminfo&#x51FD;&#x6570;&#x53D6;&#x5F97;&#x7CFB;&#x7EDF;Mem&#x4FE1;&#x606F;&#x7684;&#x56DE;&#x8FD4;&#x4FE1;&#x606F;&#x3002; &#x662F;&#x4E2A;namedtuple</span>
<span class="hljs-comment"># total, available, percent, used, free, active, inactive, buffers, cached&#x662F;&#x5979;&#x7684;&#x5C5E;&#x6027;</span>

<span class="hljs-keyword">from</span> cup.res <span class="hljs-keyword">import</span> linux
meminfo = linux.get_meminfo()
<span class="hljs-keyword">print</span> meminfo.total
<span class="hljs-keyword">print</span> meminfo.available

<span class="hljs-comment"># &#x83B7;&#x53D6;&#x7CFB;&#x7EDF;&#x5176;&#x4ED6;&#x4FE1;&#x606F;&#xFF0C; &#x5982;&#x542F;&#x52A8;&#x65F6;&#x95F4;&#x3001;cpu&#x60C5;&#x51B5;&#x3001;&#x7F51;&#x5361;&#x60C5;&#x51B5;&#x7B49;</span>
cup.res.linux.get_boottime_since_epoch()<span class="hljs-comment"># Returns:    &#x56DE;&#x8FD4;&#x81EA;&#x4ECE;epoch&#x4EE5;&#x6765;&#x7684;&#x65F6;&#x95F4;&#x3002; &#x4EE5;&#x79D2;&#x8BB0;.</span>
cup.res.linux.get_cpu_nums()[source] <span class="hljs-comment"># &#x56DE;&#x8FD4;&#x673A;&#x5668;&#x7684;CPU&#x4E2A;&#x6570;&#x4FE1;&#x606F;</span>
cup.res.linux.get_disk_usage_all() <span class="hljs-comment"># &#x62FF;&#x5230;/&#x76EE;&#x5F55;&#x7684;&#x4F7F;&#x7528;&#x4FE1;&#x606F;&#xFF0C; &#x56DE;&#x8FD4;&#x662F;&#x4E00;&#x4E2A;&#x5B57;&#x5178;</span>
cup.res.linux.get_disk_info() <span class="hljs-comment"># &#x62FF;&#x5230;Linux&#x7CFB;&#x7EDF;&#x7684;&#x6240;&#x6709;&#x78C1;&#x76D8;&#x4FE1;&#x606F;</span>

cup.res.linux.get_cpu_usage(intvl_in_sec=<span class="hljs-number">1</span>) <span class="hljs-comment"># &#x53D6;&#x5F97;&#x4E0B;&#x4E2A;intvl_in_sec&#x65F6;&#x95F4;&#x5185;&#x7684;CPU&#x4F7F;&#x7528;&#x7387;&#x4FE1;&#x606F; &#x56DE;&#x8FD4;CPUInfo</span>

cup.res.linux.get_kernel_version() <span class="hljs-comment"># &#x62FF;&#x5230;Linux&#x7CFB;&#x7EDF;&#x7684;kernel&#x4FE1;&#x606F;&#xFF0C; &#x56DE;&#x8FD4;&#x662F;&#x4E00;&#x4E2A;&#x4E09;&#x5143;&#x7EC4; e.g.(&#x2018;2&#x2019;, &#x2018;6&#x2019;, &#x2018;32&#x2019;):</span>

cup.res.linux.get_meminfo() <span class="hljs-comment"># &#x83B7;&#x5F97;&#x5F53;&#x524D;&#x7CFB;&#x7EDF;&#x7684;&#x5185;&#x5B58;&#x4FE1;&#x606F;&#xFF0C; &#x56DE;&#x8FD4;MemInfo&#x6570;&#x636E;&#x7ED3;&#x6784;&#x3002;</span>

cup.res.linux.get_net_recv_speed(str_interface, intvl_in_sec) <span class="hljs-comment"># &#x83B7;&#x5F97;&#x67D0;&#x4E2A;&#x7F51;&#x5361;&#x5728;intvl_in_sec&#x65F6;&#x95F4;&#x5185;&#x7684;&#x6536;&#x5305;&#x901F;&#x5EA6;</span>

cup.res.linux.get_net_through(str_interface) <span class="hljs-comment"># &#x83B7;&#x53D6;&#x7F51;&#x5361;&#x7684;&#x6536;&#x53D1;&#x5305;&#x7684;&#x7EDF;&#x8BA1;&#x4FE1;&#x606F;&#x3002; &#x56DE;&#x8FD4;&#x7ED3;&#x679C;&#x4E3A;(rx_bytes, tx_bytes)</span>

cup.res.linux.get_net_transmit_speed(str_interface, intvl_in_sec=<span class="hljs-number">1</span>) <span class="hljs-comment"># &#x83B7;&#x5F97;&#x7F51;&#x5361;&#x5728;intvl_in_sec&#x65F6;&#x95F4;&#x5185;&#x7684;&#x7F51;&#x7EDC;&#x53D1;&#x5305;&#x901F;&#x5EA6;</span>

<span class="hljs-keyword">print</span> cup.res.linux.get_net_transmit_speed(<span class="hljs-string">&apos;eth1&apos;</span>, <span class="hljs-number">5</span>)

cup.res.linux.get_swapinfo() <span class="hljs-comment"># &#x83B7;&#x5F97;&#x5F53;&#x524D;&#x7CFB;&#x7EDF;&#x7684;swap&#x4FE1;&#x606F;</span>

cup.res.linux.net_io_counters() <span class="hljs-comment"># net io encounters</span>
</code></pre>
<pre><code class="lang-python">
<span class="hljs-comment"># 2. &#x83B7;&#x53D6;&#x8FDB;&#x7A0B;&#x76F8;&#x5173;&#x4FE1;&#x606F;&#xFF0C; &#x79FB;&#x690D;&#x81EA;psutil&#xFF0C;&#x53EF;&#x4EE5;&#x83B7;&#x53D6;/proc/xxxpid&#x8FDB;&#x7A0B;&#x5185;&#x7684;&#x6240;&#x6709;&#x4FE1;&#x606F;</span>
<span class="hljs-comment"># &#x4E5F;&#x53EF;&#x4EE5;&#x83B7;&#x53D6;&#x8BE5;&#x8FDB;&#x7A0B;&#x4E0B;&#x6240;&#x6709;&#x5B50;&#x8FDB;&#x7A0B;&#x76F8;&#x5173;&#x7684;&#x4FE1;&#x606F;, &#x8BE6;&#x89C1;&#x4EE3;&#x7801;</span>
get_connections(*args, **kwargs)

get_cpu_times(*args, **kwargs)

get_ext_memory_info(*args, **kwargs)

get_memory_info(*args, **kwargs)

get_memory_maps()

get_num_ctx_switches(*args, **kwargs)[source]

get_num_fds(*args, **kwargs)[source]

get_open_files(*args, **kwargs)[source]

get_process_cmdline(*args, **kwargs)[source]

get_process_create_time(*args, **kwargs)[source]

get_process_cwd(*args, **kwargs)[source]

get_process_exe()[source]
&#x83B7;&#x5F97;&#x8FDB;&#x7A0B;&#x7684;Exe&#x4FE1;&#x606F;&#x3002; &#x5982;&#x679C;&#x8FD9;&#x4E2A;&#x8FDB;&#x7A0B;&#x662F;&#x4E2A;daemon&#xFF0C;&#x8BF7;&#x4F7F;&#x7528;get_process_name

get_process_gids(*args, **kwargs)[source]

get_process_io_counters(*args, **kwargs)[source]

get_process_name(*args, **kwargs)[source]

get_process_nice(*args, **kwargs)[source]

get_process_num_threads(*args, **kwargs)[source]

get_process_ppid(*args, **kwargs)[source]

get_process_status(*args, **kwargs)[source]

get_process_threads(*args, **kwargs)[source]

get_process_uids(*args, **kwargs)[source]

nt_mmap_ext alias of mmap

nt_mmap_grouped alias of mmap
</code></pre>
<h2 id="14-cupshell-&#x64CD;&#x4F5C;shell&#x547D;&#x4EE4;&#x76F8;&#x5173;&#x7684;package"><a name="14-cupshell-&#x64CD;&#x4F5C;shell&#x547D;&#x4EE4;&#x76F8;&#x5173;&#x7684;package" class="plugin-anchor" href="#14-cupshell-&#x64CD;&#x4F5C;shell&#x547D;&#x4EE4;&#x76F8;&#x5173;&#x7684;package"><i class="fa fa-link" aria-hidden="true"></i></a>1.4 cup.shell &#x64CD;&#x4F5C;shell&#x547D;&#x4EE4;&#x76F8;&#x5173;&#x7684;package.</h2>
<p><strong>&#x4EFB;&#x4F55;shell&#x64CD;&#x4F5C;&#x90FD;&#x5E94;&#x8BE5;&#x8003;&#x8651;&#x8D85;&#x65F6;&#x673A;&#x5236;!!!</strong></p>
<blockquote>
<p>&#x5426;&#x5219;&#x53EF;&#x80FD;&#x5BFC;&#x81F4;&#x67D0;&#x5355;&#x4E2A;shell cmd &#x6267;&#x884C; hang &#x4F4F;, &#x6574;&#x4E2A;&#x7EBF;&#x7A0B;/&#x8FDB;&#x7A0B;&#x5361;&#x4F4F;.
shell.ShellExec&#x7C7B;&#x5C31;&#x662F;&#x4E3A;&#x4E86;&#x8BE5;&#x7406;&#x5FF5;&#x800C;&#x8BBE;&#x8BA1;, &#x652F;&#x6301;&#x8D85;&#x65F6;auto-kill&#x64CD;&#x4F5C;</p>
</blockquote>
<p> &#x5177;&#x4F53;&#x6587;&#x6863;&#x4ECB;&#x7ECD;</p>
<blockquote>
<p><a href="http://cupdoc.iobusy.com/cup.shell/" target="_blank">http://cupdoc.iobusy.com/cup.shell/</a></p>
</blockquote>
<p>  &#x4EE3;&#x7801;&#x793A;&#x4F8B;</p>
<pre><code class="lang-python"><span class="hljs-comment"># &#x7279;&#x8272;&#x793A;&#x4F8B;</span>
<span class="hljs-comment"># 1. &#x62E5;&#x6709;&#x8D85;&#x65F6;&#x63A7;&#x5236;&#x7684;shell&#x6267;&#x884C;. &#x8D85;&#x65F6;shell&#x547D;&#x4EE4;&#x4F1A;&#x88AB;kill (SIGTERM)</span>
<span class="hljs-keyword">import</span> cup
shelltool = cup.shell.ShellExec()
<span class="hljs-keyword">print</span> shelltool.run(<span class="hljs-string">&apos;/bin/ls&apos;</span>, timeout=<span class="hljs-number">1</span>)

<span class="hljs-comment"># 2. &#x5176;&#x4ED6;&#x7C7B;&#xFF0C;&#x6BD4;&#x5982;&#x4F7F;&#x7528;rm -rf&#x5220;&#x9664;&#x67D0;&#x8DEF;&#x5F84;&#xFF0C;&#x83B7;&#x53D6;&#x6587;&#x4EF6;MD5&#x7B49;</span>
cup.shell.md5file(filename) <span class="hljs-comment"># &#x8BA1;&#x7B97;&#x4E00;&#x4E2A;&#x6587;&#x4EF6;&#x7684;md5&#x503C;&#x3002;&#x8FD4;&#x56DE;32&#x4F4D;&#x957F;&#x7684;hex&#x5B57;&#x7B26;&#x4E32;&#x3002;</span>

cup.shell.kill9_byname(strname) <span class="hljs-comment"># kill -9 process by name</span>

cup.shell.del_if_exist(path) <span class="hljs-comment"># &#x5982;&#x679C;&#x6587;&#x4EF6;/&#x76EE;&#x5F55;/symlink&#x5B58;&#x5728;&#x5219;&#x5220;&#x9664;&#x4ED6;&#x4EEC;</span>

<span class="hljs-comment"># 3. &#x8FDB;&#x884C;&#x8FDC;&#x7A0B;shell&#x64CD;&#x4F5C; &#xFF08;&#x5185;&#x90E8;&#x8C03;&#x7528;&#x4E86;pexpect)</span>

<span class="hljs-comment"># &#x5177;&#x4F53;&#x53C2;&#x7167; http://cupdoc.iobusy.com/cup.shell/#module-cup.shell.expect</span>
<span class="hljs-comment"># &#x5176;&#x4E2D;exit_status&#x4E3A;&#x672C;&#x5730;ssh&#x547D;&#x4EE4;&#x9000;&#x51FA;&#x7801;&#xFF0C; remote_status&#x4E3A;&#x8FDC;&#x7A0B;&#x7684;&#x9000;&#x51FA;&#x7801;</span>
</code></pre>
<h2 id="15-cuputil-util&#x7C7B;package---rich-configurationcontext"><a name="15-cuputil-util&#x7C7B;package---rich-configurationcontext" class="plugin-anchor" href="#15-cuputil-util&#x7C7B;package---rich-configurationcontext"><i class="fa fa-link" aria-hidden="true"></i></a>1.5 cup.util util&#x7C7B;package - Rich Configuration/Context</h2>
<blockquote>
<p>&#x5404;&#x7C7B;util&#x7C7B;pacakge</p>
<ul>
<li>&#x5BCC;&#x914D;&#x7F6E;&#x7C7B;&#x578B;, &#x53C2;&#x89C1;&#x4E0B;&#x9762;&#x4ECB;&#x7ECD;</li>
<li>&#x7EBF;&#x7A0B;&#x4E0A;&#x4E0B;&#x6587;</li>
</ul>
</blockquote>
<p> &#x5177;&#x4F53;&#x6587;&#x6863;&#x4ECB;&#x7ECD;</p>
<blockquote>
<p><a href="http://cupdoc.iobusy.com/cup.util/" target="_blank">http://cupdoc.iobusy.com/cup.util/</a>  &#x603B;&#x89C8;</p>
</blockquote>
<pre><code class="lang-python">
<span class="hljs-comment">#1 &#x652F;&#x6301;&#x7C7B;&#x578B;&#x4E30;&#x5BCC;&#x3001;&#x5E26;&#x5185;&#x5D4C;&#x5957;&#x3001;&#x6570;&#x7EC4;&#x7684;&#x6587;&#x6863; http://cupdoc.iobusy.com/cup.util/#cup.util.conf.Configure2Dict</span>
<span class="hljs-comment"># test.conf</span>
<span class="hljs-comment"># &#x7B2C;&#x4E00;&#x5C42;&#xFF0C;Global layer, key:value</span>
host: abc.com
port: <span class="hljs-number">12345</span>
<span class="hljs-comment"># 1st layer [monitor]</span>
<span class="hljs-meta">@disk: sda</span>
<span class="hljs-meta">@disk: sdb</span>

<span class="hljs-comment"># &#x7B2C;&#x4E8C;&#x5C42;&#x914D;&#x7F6E; conf_dict[&apos;section&apos;]</span>
[section]
    <span class="hljs-comment"># &#x652F;&#x6301;&#x6570;&#x7EC4; conf_dict[&apos;section&apos;][&apos;disk&apos;] =&gt;&#x662F;&#x4E2A;&#x4E00;&#x7EF4;&#x6570;&#x7EC4;</span>
<span class="hljs-meta">    @disk: sda</span>
<span class="hljs-meta">    @disk: sdb</span>
    <span class="hljs-comment"># &#x652F;&#x6301;&#x5C42;&#x7EA7;&#x6570;&#x7EC4;  conf_dict[&apos;section&apos;][&apos;ioutil&apos;] =&gt; &#x662F;&#x4E2A;&#x4E00;&#x7EF4;&#x6570;&#x7EC4;</span>
    [@ioutil]
        util: <span class="hljs-number">80</span>   <span class="hljs-comment"># accessed by conf_dict[&apos;section&apos;][&apos;ioutil&apos;][0][&apos;util&apos;]</span>
    [@ioutil]
        util: <span class="hljs-number">60</span>


[monitor]
    timeout: <span class="hljs-number">100</span>
    regex:  sshd
    <span class="hljs-comment"># 2nd layer that belongs to [monitor]</span>
    <span class="hljs-comment"># &#x7B2C;&#x4E09;&#x5C42;&#x914D;&#x7F6E;  conf_dict[&apos;monitor&apos;][&apos;timeout&apos;]</span>
    [.timeout]
        <span class="hljs-comment"># key:value in timeout</span>
        max: <span class="hljs-number">100</span>
        <span class="hljs-comment"># 3rd layer that belongs to [monitor] [timeout]</span>
        [..handler]
            default: exit


<span class="hljs-comment"># 3. &#x751F;&#x6210;&#x552F;&#x4E00;&#x7801;&#x3001;&#x552F;&#x4E00;&#x6570;&#x7B49;</span>
<span class="hljs-comment"># http://cupdoc.iobusy.com/cup.services/#module-cup.services.generator</span>
</code></pre>
<h2 id="16-cupnet-&#x7F51;&#x7EDC;&#x64CD;&#x4F5C;&#x76F8;&#x5173;&#x7684;package"><a name="16-cupnet-&#x7F51;&#x7EDC;&#x64CD;&#x4F5C;&#x76F8;&#x5173;&#x7684;package" class="plugin-anchor" href="#16-cupnet-&#x7F51;&#x7EDC;&#x64CD;&#x4F5C;&#x76F8;&#x5173;&#x7684;package"><i class="fa fa-link" aria-hidden="true"></i></a>1.6 cup.net &#x7F51;&#x7EDC;&#x64CD;&#x4F5C;&#x76F8;&#x5173;&#x7684;package.</h2>
<blockquote>
<p>&#x4E24;&#x4E2A;&#x91CD;&#x8981;&#x90E8;&#x5206;&#xFF1A;</p>
</blockquote>
<ul>
<li>a. &#x7F51;&#x5361;&#x4FE1;&#x606F;&#x83B7;&#x53D6;&#x3001;&#x8DEF;&#x7531;&#x4FE1;&#x606F;&#x83B7;&#x53D6;&#x3001;socket&#x53C2;&#x6570;&#x8BBE;&#x7F6E;</li>
<li><p>b. &#x5F02;&#x6B65;&#x6D88;&#x606F;&#x901A;&#x4FE1;&#x534F;&#x8BAE;&#x652F;&#x6301;</p>
<h3 id="161-&#x7F51;&#x5361;&#x4FE1;&#x606F;&#x83B7;&#x53D6;&#x3001;&#x8DEF;&#x7531;&#x4FE1;&#x606F;&#x83B7;&#x53D6;&#x3001;socket&#x8BBE;&#x7F6E;"><a name="161-&#x7F51;&#x5361;&#x4FE1;&#x606F;&#x83B7;&#x53D6;&#x3001;&#x8DEF;&#x7531;&#x4FE1;&#x606F;&#x83B7;&#x53D6;&#x3001;socket&#x8BBE;&#x7F6E;" class="plugin-anchor" href="#161-&#x7F51;&#x5361;&#x4FE1;&#x606F;&#x83B7;&#x53D6;&#x3001;&#x8DEF;&#x7531;&#x4FE1;&#x606F;&#x83B7;&#x53D6;&#x3001;socket&#x8BBE;&#x7F6E;"><i class="fa fa-link" aria-hidden="true"></i></a>1.6.1 &#x7F51;&#x5361;&#x4FE1;&#x606F;&#x83B7;&#x53D6;&#x3001;&#x8DEF;&#x7531;&#x4FE1;&#x606F;&#x83B7;&#x53D6;&#x3001;socket&#x8BBE;&#x7F6E;</h3>
<p>&#x5177;&#x4F53;&#x6587;&#x6863;&#x4ECB;&#x7ECD;</p>
</li>
</ul>
<blockquote>
<p><a href="http://cupdoc.iobusy.com/cup.net/" target="_blank">http://cupdoc.iobusy.com/cup.net/</a></p>
</blockquote>
<p> &#x4EE3;&#x7801;&#x793A;&#x4F8B;&#xFF1A;</p>
<pre><code class="lang-python"><span class="hljs-comment"># 1. &#x83B7;&#x53D6;&#x8DEF;&#x7531;&#x4FE1;&#x606F;</span>
<span class="hljs-keyword">from</span> cup.net <span class="hljs-keyword">import</span> route
ri = route.RouteInfo()
<span class="hljs-keyword">print</span> json.dumps(ri.get_route_by_ip(<span class="hljs-string">&apos;10.32.19.92&apos;</span>), indent=<span class="hljs-number">1</span>)
<span class="hljs-keyword">print</span> json.dumps(ri.get_routes(), indent=<span class="hljs-number">1</span>)

{
 <span class="hljs-string">&quot;Use&quot;</span>: <span class="hljs-string">&quot;0&quot;</span>,
 <span class="hljs-string">&quot;Iface&quot;</span>: <span class="hljs-string">&quot;eth1&quot;</span>,
 <span class="hljs-string">&quot;Metric&quot;</span>: <span class="hljs-string">&quot;0&quot;</span>,
 <span class="hljs-string">&quot;Destination&quot;</span>: <span class="hljs-string">&quot;10.0.0.0&quot;</span>,
 <span class="hljs-string">&quot;Mask&quot;</span>: <span class="hljs-string">&quot;255.0.0.0&quot;</span>,
 <span class="hljs-string">&quot;RefCnt&quot;</span>: <span class="hljs-string">&quot;0&quot;</span>,
 <span class="hljs-string">&quot;MTU&quot;</span>: <span class="hljs-string">&quot;0&quot;</span>,
 <span class="hljs-string">&quot;Window&quot;</span>: <span class="hljs-string">&quot;0&quot;</span>,
 <span class="hljs-string">&quot;Gateway&quot;</span>: <span class="hljs-string">&quot;10.226.71.1&quot;</span>,
 <span class="hljs-string">&quot;Flags&quot;</span>: <span class="hljs-string">&quot;0003&quot;</span>,
 <span class="hljs-string">&quot;IRTT&quot;</span>: <span class="hljs-string">&quot;0&quot;</span>
}
[
 {
  <span class="hljs-string">&quot;Use&quot;</span>: <span class="hljs-string">&quot;0&quot;</span>,
  <span class="hljs-string">&quot;Iface&quot;</span>: <span class="hljs-string">&quot;eth1&quot;</span>,
  <span class="hljs-string">&quot;Metric&quot;</span>: <span class="hljs-string">&quot;0&quot;</span>,
  <span class="hljs-string">&quot;Destination&quot;</span>: <span class="hljs-string">&quot;10.226.71.0&quot;</span>,
  <span class="hljs-string">&quot;Mask&quot;</span>: <span class="hljs-string">&quot;255.255.255.0&quot;</span>,
  <span class="hljs-string">&quot;RefCnt&quot;</span>: <span class="hljs-string">&quot;0&quot;</span>,
  <span class="hljs-string">&quot;MTU&quot;</span>: <span class="hljs-string">&quot;0&quot;</span>,
  <span class="hljs-string">&quot;Window&quot;</span>: <span class="hljs-string">&quot;0&quot;</span>,
  <span class="hljs-string">&quot;Gateway&quot;</span>: <span class="hljs-string">&quot;0.0.0.0&quot;</span>,
  <span class="hljs-string">&quot;Flags&quot;</span>: <span class="hljs-string">&quot;0001&quot;</span>,
  <span class="hljs-string">&quot;IRTT&quot;</span>: <span class="hljs-string">&quot;0&quot;</span>
 },

<span class="hljs-comment"># 2. &#x83B7;&#x53D6;ip &amp;&amp; &#x8BBE;&#x7F6E;socket&#x53C2;&#x6570;&#x7B49;</span>
<span class="hljs-keyword">from</span> cup <span class="hljs-keyword">import</span> net

<span class="hljs-comment"># set up socket params</span>
net.set_sock_keepalive_linux(sock, <span class="hljs-number">1</span>, <span class="hljs-number">3</span>, <span class="hljs-number">3</span>)
net.set_sock_linger(sock)
net.set_sock_quickack(sock)
net.set_sock_reusable(sock, <span class="hljs-keyword">True</span>)  <span class="hljs-comment"># port resuable</span>

<span class="hljs-comment"># get ipaddr of a network adapter/interface</span>
<span class="hljs-keyword">print</span> net.getip_byinterface(<span class="hljs-string">&apos;eth0&apos;</span>)
<span class="hljs-keyword">print</span> net.getip_byinterface(<span class="hljs-string">&apos;eth1&apos;</span>)
<span class="hljs-keyword">print</span> net.getip_byinterface(<span class="hljs-string">&apos;xgbe0&apos;</span>)   <span class="hljs-comment"># &#x4E07;&#x5146;&#x7F51;&#x5361;</span>

<span class="hljs-comment"># get ipaddr of a hostname</span>
<span class="hljs-keyword">print</span> net.get_hostip(<span class="hljs-string">&apos;abc.test.com&apos;</span>)
</code></pre>
<h3 id="162-&#x5F02;&#x6B65;&#x7F51;&#x7EDC;&#x6D88;&#x606F;&#x901A;&#x4FE1;&#x5E93;"><a name="162-&#x5F02;&#x6B65;&#x7F51;&#x7EDC;&#x6D88;&#x606F;&#x901A;&#x4FE1;&#x5E93;" class="plugin-anchor" href="#162-&#x5F02;&#x6B65;&#x7F51;&#x7EDC;&#x6D88;&#x606F;&#x901A;&#x4FE1;&#x5E93;"><i class="fa fa-link" aria-hidden="true"></i></a>1.6.2 &#x5F02;&#x6B65;&#x7F51;&#x7EDC;&#x6D88;&#x606F;&#x901A;&#x4FE1;&#x5E93;</h3>
<p> &#x5177;&#x4F53;&#x6587;&#x6863;&#x4ECB;&#x7ECD;</p>
<blockquote>
<p><a href="http://cupdoc.iobusy.com/cup.net.async/#module-cup.net.async.msgcenter" target="_blank">http://cupdoc.iobusy.com/cup.net.async/#module-cup.net.async.msgcenter</a></p>
</blockquote>
<p> &#x5177;&#x4F53;&#x4ECB;&#x7ECD;&#xFF1A;</p>
<ul>
<li>&#x8BF7;&#x79FB;&#x6B65;&#x5DE6;&#x4FA7;&#x5173;&#x4E8E;&#x7F51;&#x7EDC;&#x901A;&#x4FE1;&#x534F;&#x8BAE;&#x4ECB;&#x7ECD;</li>
</ul>
<p>&#x5F02;&#x6B65;&#x6D88;&#x606F;&#x5E93;&#x5C5E;&#x4E8E;&#x4E13;&#x7528;&#x4E14;&#x8F83;&#x4E3A;&#x590D;&#x6742;&#x7684;&#x901A;&#x4FE1;&#x5E93;&#xFF0C;&#x4E3A;&#x9AD8;&#x541E;&#x5410;&#x3001;&#x9AD8;&#x6548;&#x7F51;&#x7EDC;&#x901A;&#x4FE1;&#x573A;&#x666F;&#x4F7F;&#x7528;&#xFF0C;&#x8BF7;&#x6CE8;&#x610F;&#x4F60;&#x7684;&#x4F7F;&#x7528;&#x573A;&#x666F;&#x662F;&#x5426;&#x5339;&#x914D;</p>
<h2 id="17-cupdecorators-&#x8FDB;&#x884C;&#x51FD;&#x6570;&#x3001;&#x7C7B;&#x4FEE;&#x9970;&#x7684;module"><a name="17-cupdecorators-&#x8FDB;&#x884C;&#x51FD;&#x6570;&#x3001;&#x7C7B;&#x4FEE;&#x9970;&#x7684;module" class="plugin-anchor" href="#17-cupdecorators-&#x8FDB;&#x884C;&#x51FD;&#x6570;&#x3001;&#x7C7B;&#x4FEE;&#x9970;&#x7684;module"><i class="fa fa-link" aria-hidden="true"></i></a>1.7 cup.decorators &#x8FDB;&#x884C;&#x51FD;&#x6570;&#x3001;&#x7C7B;&#x4FEE;&#x9970;&#x7684;module</h2>
<p>&#x4E3E;&#x4F8B;&#xFF0C;&#x4E00;&#x952E;&#x53D8;&#x7C7B;&#x4E3A;Singleton&#x7C7B;&#x3002;</p>
<h2 id="18-cupjenkinslib-&#x4E0E;&#x6301;&#x7EED;&#x96C6;&#x6210;&#x5E73;&#x53F0;&#x8FDB;&#x884C;api&#x64CD;&#x4F5C;"><a name="18-cupjenkinslib-&#x4E0E;&#x6301;&#x7EED;&#x96C6;&#x6210;&#x5E73;&#x53F0;&#x8FDB;&#x884C;api&#x64CD;&#x4F5C;" class="plugin-anchor" href="#18-cupjenkinslib-&#x4E0E;&#x6301;&#x7EED;&#x96C6;&#x6210;&#x5E73;&#x53F0;&#x8FDB;&#x884C;api&#x64CD;&#x4F5C;"><i class="fa fa-link" aria-hidden="true"></i></a>1.8 cup.jenkinslib &#x4E0E;&#x6301;&#x7EED;&#x96C6;&#x6210;&#x5E73;&#x53F0;&#x8FDB;&#x884C;API&#x64CD;&#x4F5C;</h2>
<p> &#x4EE3;&#x7801;&#x793A;&#x4F8B;&#xFF1A;</p>
<pre><code class="lang-python"><span class="hljs-keyword">import</span> cup
<span class="hljs-keyword">import</span> cup.jenkinslib

<span class="hljs-comment">############### quick start ###############</span>
jenkins = cup.jenkinslib.Jenkins(<span class="hljs-string">&apos;cup.jenkins.baidu.com&apos;</span>)

job = jenkins[<span class="hljs-string">&apos;cup_quick&apos;</span>]
<span class="hljs-keyword">print</span> job.name, job.last_stable_build_number, job.description
<span class="hljs-keyword">print</span> job[<span class="hljs-number">5</span>], job[<span class="hljs-string">&quot;lastSuccessBuild&quot;</span>], job.last_stable_build

qi = job.invoke()
build = qi.block_until_building()
<span class="hljs-keyword">print</span> build.name, build.number, build.timestamp

<span class="hljs-keyword">try</span>:
    build.block_until_complete(timeout=<span class="hljs-number">20</span>)
<span class="hljs-keyword">except</span> cup.jenkinslib.RunTimeout <span class="hljs-keyword">as</span> err:
    <span class="hljs-keyword">print</span> <span class="hljs-string">&quot;timeout:&quot;</span>, err
    build.stop()

<span class="hljs-keyword">print</span> build.duration, build.result, build.description

build.description = <span class="hljs-string">&quot;new description&quot;</span>

jenkins.enable_ftp(<span class="hljs-string">&apos;ftp.baidu.com&apos;</span>, <span class="hljs-string">&apos;cup&apos;</span>, <span class="hljs-string">&apos;password&apos;</span>, <span class="hljs-number">22</span>)
<span class="hljs-keyword">with</span> build.ftp_artifacts <span class="hljs-keyword">as</span> af:
    af[<span class="hljs-string">&apos;artifacts_path&apos;</span>].download(<span class="hljs-string">&apos;./local_path&apos;</span>)
</code></pre>
<h2 id="19-cupexfile"><a name="19-cupexfile" class="plugin-anchor" href="#19-cupexfile"><i class="fa fa-link" aria-hidden="true"></i></a>1.9 cup.exfile</h2>
<p>exfile &#x4EE3;&#x8868;extension of file related objects. &#x76EE;&#x524D;&#x652F;&#x6301;:</p>
<ul>
<li>LockFile, &#x72EC;&#x5360;&#x5F0F;&#x9501;&#x6587;&#x4EF6;</li>
<li>TempFile&#xFF0C;&#x4E34;&#x65F6;&#x6587;&#x4EF6;&#xFF0C;&#x5728;&#x751F;&#x547D;&#x5468;&#x671F;&#x5230;&#x671F;&#x540E;&#x81EA;&#x52A8;&#x5220;&#x9664;</li>
</ul>
<p>a. LockFile&#x4EE3;&#x7801;&#x793A;&#x4F8B;&#xFF1A;</p>
<pre><code class="lang-python"><span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">test_sharedlockfile</span><span class="hljs-params">()</span>:</span>
    <span class="hljs-string">&quot;&quot;&quot;test lockfile&quot;&quot;&quot;</span>
    <span class="hljs-comment"># shared lockfile</span>
    _lockfile = exfile.LockFile(LOCK_FILE, exfile.FILELOCK_SHARED)
    ret = _lockfile.lock(blocking=<span class="hljs-keyword">False</span>)
    <span class="hljs-keyword">print</span> ret
    _lockfile2 = exfile.LockFile(LOCK_FILE, exfile.FILELOCK_SHARED)
    ret = _lockfile2.lock(blocking=<span class="hljs-keyword">False</span>)
    <span class="hljs-keyword">print</span> ret
    _lockfile3 = exfile.LockFile(LOCK_FILE, exfile.FILELOCK_EXCLUSIVE)
    unittest.expect_raise(
        _lockfile3.lock,
        err.LockFileError,
        <span class="hljs-keyword">False</span>
    )

<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">test_exclusive_lockfile</span><span class="hljs-params">()</span>:</span>
    <span class="hljs-string">&quot;&quot;&quot;test exclusive lockfile&quot;&quot;&quot;</span>
    <span class="hljs-comment"># shared lockfile</span>
    _lockfile = exfile.LockFile(LOCK_FILE, exfile.FILELOCK_EXCLUSIVE)
    _lockfile.lock()
    _lockfile2 = exfile.LockFile(LOCK_FILE, exfile.FILELOCK_EXCLUSIVE)
    unittest.expect_raise(
        _lockfile2.lock,
        err.LockFileError,
        blocking=<span class="hljs-keyword">False</span>
    )
    _lockfile3 = exfile.LockFile(LOCK_FILE, exfile.FILELOCK_SHARED)
    unittest.expect_raise(
        _lockfile3.lock,
        err.LockFileError,
        blocking=<span class="hljs-keyword">False</span>
    )
</code></pre>
<p>b. TempFile&#x793A;&#x4F8B;</p>
<pre><code class="lang-python"><span class="hljs-comment"># &#x8BF7;&#x6CE8;&#x610F;&#x5728;tmp&#x53D8;&#x91CF;&#x751F;&#x547D;&#x5468;&#x671F;&#x7ED3;&#x675F;&#x540E;&#xFF0C; &#x8BE5;temp&#x6587;&#x4EF6;&#x4F1A;&#x88AB;&#x7ACB;&#x523B;&#x5220;&#x9664;</span>
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">test_func</span><span class="hljs-params">()</span>:</span>
    tmp = exfile.TempFile()
    tmp.write/read/etc
    tmp.close()
</code></pre>
<h2 id="110-cupthirdp-&#x7B2C;&#x4E09;&#x65B9;&#x7684;&#x5E93;&#x4F9D;&#x8D56;&#x652F;&#x6301;"><a name="110-cupthirdp-&#x7B2C;&#x4E09;&#x65B9;&#x7684;&#x5E93;&#x4F9D;&#x8D56;&#x652F;&#x6301;" class="plugin-anchor" href="#110-cupthirdp-&#x7B2C;&#x4E09;&#x65B9;&#x7684;&#x5E93;&#x4F9D;&#x8D56;&#x652F;&#x6301;"><i class="fa fa-link" aria-hidden="true"></i></a>1.10 cup.thirdp &#x7B2C;&#x4E09;&#x65B9;&#x7684;&#x5E93;&#x4F9D;&#x8D56;&#x652F;&#x6301;</h2>
<ul>
<li>httplib2</li>
<li>requests</li>
<li>MySQLdb (pymysql inside)</li>
<li>pexpect</li>
</ul>

                                
                                </section>
                            
    </div>
    <div class="search-results">
        <div class="has-results">
            
            <h1 class="search-results-title"><span class='search-results-count'></span> results matching "<span class='search-query'></span>"</h1>
            <ul class="search-results-list"></ul>
            
        </div>
        <div class="no-results">
            
            <h1 class="search-results-title">No results matching "<span class='search-query'></span>"</h1>
            
        </div>
    </div>
</div>

                        </div>
                    </div>
                
            </div>

            
                
                
                <a href="CommonService.html" class="navigation navigation-next navigation-unique" aria-label="Next page: 通用服务">
                    <i class="fa fa-angle-right"></i>
                </a>
                
            
        
    </div>

    <script>
        var gitbook = gitbook || [];
        gitbook.push(function() {
            gitbook.page.hasChanged({"page":{"title":"基础功能","level":"1.2.1","depth":2,"next":{"title":"通用服务","level":"1.2.2","depth":2,"path":"senarios/CommonService.md","ref":"senarios/CommonService.md","articles":[]},"previous":{"title":"用户案例","level":"1.2","depth":1,"ref":"","articles":[{"title":"基础功能","level":"1.2.1","depth":2,"path":"senarios/CommonUserSenario.md","ref":"senarios/CommonUserSenario.md","articles":[]},{"title":"通用服务","level":"1.2.2","depth":2,"path":"senarios/CommonService.md","ref":"senarios/CommonService.md","articles":[]},{"title":"快速构建PythonService","level":"1.2.3","depth":2,"path":"senarios/GeneralService.md","ref":"senarios/GeneralService.md","articles":[]},{"title":"异步网络通讯协议","level":"1.2.4","depth":2,"path":"senarios/AsyncNet.md","ref":"senarios/AsyncNet.md","articles":[]}]},"dir":"ltr"},"config":{"plugins":["-sharing","splitter","anchors","sharing-plus","favicon","insert-logo","chinese-video","github-buttons"],"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"pluginsConfig":{"splitter":{},"search":{},"sharing-plus":{"qq":false,"all":["facebook","google","twitter","instapaper","linkedin","pocket","stumbleupon"],"douban":false,"facebook":true,"weibo":false,"instapaper":false,"whatsapp":false,"hatenaBookmark":false,"twitter":true,"messenger":false,"line":false,"vk":false,"pocket":true,"google":false,"viber":false,"stumbleupon":false,"qzone":false,"linkedin":false},"lunr":{"maxIndexSize":1000000,"ignoreSpecialCharacters":false},"fontsettings":{"theme":"white","family":"sans","size":2},"highlight":{},"anchor-navigation-ex":{"showLevel":false,"mode":"float","pageTop":{"showLevelIcon":true,"level1Icon":"fa fa-hand-o-right","level2Icon":"fa fa-hand-o-right","level3Icon":"fa fa-hand-o-right"}},"favicon":{"shortcut":"./images/cup.logo.jpg","bookmark":"./images/cup.logo.jpg","appleTouch":"./images/cup.logo.jpg"},"github-buttons":{"repo":"baidu/CUP","types":["star","watch","fork"],"size":"large"},"chinese-video":{},"sharing":{"qq":false,"all":["google","facebook","weibo","twitter","qq","qzone","linkedin","pocket"],"douban":false,"facebook":false,"weibo":false,"instapaper":false,"whatsapp":false,"hatenaBookmark":false,"twitter":false,"messenger":false,"line":false,"vk":false,"pocket":false,"google":false,"viber":false,"stumbleupon":false,"qzone":false,"linkedin":false},"theme-default":{"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"showLevel":false},"anchors":{},"insert-logo":{"style":"background: none;","url":"http://cup.iobusy.com/cup.logo.png"}},"theme":"default","author":"Guannan Ma","pdf":{"pageNumbers":true,"fontSize":12,"fontFamily":"Arial","paperSize":"a4","chapterMark":"pagebreak","pageBreaksBefore":"/","margin":{"right":62,"left":62,"top":56,"bottom":56}},"structure":{"langs":"LANGS.md","readme":"README.md","glossary":"GLOSSARY.md","summary":"SUMMARY.md"},"variables":{},"title":"Cup Documentation","language":"zh-hans","links":{"sidebar":{"Github Website":"https://github.com/baidu/CUP"}},"gitbook":"*","description":"Cup Documentation which you may refer to"},"file":{"path":"senarios/CommonUserSenario.md","mtime":"2019-05-10T11:40:02.249Z","type":"markdown"},"gitbook":{"version":"3.2.3","time":"2020-07-14T09:31:09.653Z"},"basePath":"..","book":{"language":"zh-hans"}});
        });
    </script>
</div>

        
    <script src="../../gitbook/gitbook.js"></script>
    <script src="../../gitbook/theme.js"></script>
    
        
        <script src="../../gitbook/gitbook-plugin-splitter/splitter.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-sharing-plus/buttons.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-insert-logo/plugin.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-github-buttons/plugin.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-search/search-engine.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-search/search.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-lunr/lunr.min.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-lunr/search-lunr.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-fontsettings/fontsettings.js"></script>
        
    

    </body>
</html>

